.PHONY: all clean

TOP=.
BUILD_DIR=./build
BIN_DIR=./bin
LOG_DIR=./logs

INCLUDE_DIR=$(BUILD_DIR)/include
BUILD_CLUALIB_DIR=$(BUILD_DIR)/clualib
BUILD_CSERVICE_DIR=$(BUILD_DIR)/cservice
BUILD_CLIB_DIR=$(BUILD_DIR)/clib

LUA_BIN="./skynet/3rd/lua/lua"
export LUA_CPATH=$(TOP)/$(BUILD_CLUALIB_DIR)/?.so
export LUA_PATH=$(TOP)/lualib/?.lua;$(TOP)/skynet/lualib/?.lua;

all: build

build:
	-mkdir -p $(BUILD_DIR)
	-mkdir -p $(BIN_DIR)
	-mkdir -p ${LOG_DIR}
	-mkdir -p $(INCLUDE_DIR)
	-mkdir -p $(BUILD_CLUALIB_DIR)
	-mkdir -p $(BUILD_CSERVICE_DIR)
	-mkdir -p $(BUILD_CLIB_DIR)

.PHONY: skynet bewater

all: skynet bewater

SKYNET_MAKEFILE=skynet/Makefile
BEWATER_MAKEFILE=bewater/Makefile

SKYNET_DEP_PATH=SKYNET_BUILD_PATH=../$(BIN_DIR) \
				LUA_CLIB_PATH=../$(BUILD_CLUALIB_DIR) \
				CSERVICE_PATH=../$(BUILD_CSERVICE_DIR)


$(SKYNET_MAKEFILE):
	git submodule update --init

build-skynet: | $(SKYNET_MAKEFILE)
	cd skynet && $(MAKE) PLAT=linux $(SKYNET_DEP_PATH)

skynet: build-skynet
	cp skynet/skynet-src/skynet_malloc.h $(INCLUDE_DIR)
	cp skynet/skynet-src/skynet.h $(INCLUDE_DIR)
	cp skynet/skynet-src/skynet_env.h $(INCLUDE_DIR)
	cp skynet/skynet-src/skynet_socket.h $(INCLUDE_DIR)
	cp skynet/3rd/lua/lua.h $(INCLUDE_DIR)
	cp skynet/3rd/lua/lauxlib.h $(INCLUDE_DIR)
	cp skynet/3rd/lua/lualib.h $(INCLUDE_DIR)
	cp skynet/3rd/lua/luaconf.h $(INCLUDE_DIR)
	cp skynet/3rd/lua/lua $(BIN_DIR)

build-bewater: | $(BEWATER_MAKEFILE)
	cd bewater && $(MAKE) build && $(MAKE) lib

bewater: build-bewater
	cp bewater/luaclib/* $(BUILD_CLUALIB_DIR)

clean:
	-rm -rf $(BUILD_DIR) $(BIN_DIR) $(LOG_DIR)

